package day021.tcp.demo4;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;

public class TcpServer {
    private static final Logger LOGGER = LoggerFactory.getLogger("TcpServer服务端");

    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(9090);
        LOGGER.trace("服务端程序已启动...");

        while (true) {
            Socket server = ss.accept();
            LOGGER.trace(server.getInetAddress().getHostAddress() + "客户端连接成功...");

            InputStream netInput = server.getInputStream();

            String fileName = UUID.randomUUID().toString().replace("-", "");
            FileOutputStream fos = new FileOutputStream("D:\\ideaProjects\\javase_code\\study-practice\\src" +
                    "\\day021\\tcp\\upload\\123.jpg");

            byte[] buf = new byte[1024];
            int len;
            while ((len = netInput.read(buf)) != -1) {
                fos.write(buf, 0, len);
            }
            LOGGER.trace("文件上传成功...");

            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(server.getOutputStream()));
            bw.write("上传成功！");

            bw.close();
            fos.close();
            netInput.close();
            server.close();
        }
    }
}
